package edu.ntu.cloudroid.model.data;
import java.io.PrintWriter;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.IDs.UploadID;
import edu.ntu.cloudroid.nongui.com.Communicator;
import edu.ntu.cloudroid.server.UploadServlet;

public class VitalSignInfo implements DetachedData, Serializable{
	private String name;
	private Date timestamp;	
	private double heartRate, oxygenVolumne; 
	private double lowHR, highHR, lowOV, highOV; //for range result;
	private boolean isRangeResult;
	
	public VitalSignInfo(String name, Date ts, double hr, double bp){
		this.name = name;
		this.timestamp = ts;		
		this.heartRate = hr;
		this.oxygenVolumne = bp;
		this.isRangeResult = false;
	}

	public VitalSignInfo(String name, Date ts, double lHR, double hHR,
			double lBP, double hBP) {
		this.name = name;
		this.timestamp = ts;
		this.lowOV = lBP;
		this.highOV = hBP;
		this.lowHR = lHR;
		this.highHR = hHR;
		this.isRangeResult = true;
	}
	
	public String getName() {
		return name;
	}

	public Date getTimestamp() {
		return timestamp;
	}
		

	public double getLowHR() {
		return lowHR;
	}

	public double getHighHR() {
		return highHR;
	}

	public double getLowOV() {
		return lowOV;
	}

	public double getHighOV() {
		return highOV;
	}

	public boolean isRangeResult() {
		return isRangeResult;
	}

	public double getHeartRate() {
		return heartRate;
	}

	public double getOxygenVolume() {
		return oxygenVolumne;
	}

	@Override
	public AbstractData toGAEData() throws Exception {
		UploadServlet.logger.info("Instantiating Abstract Data");
		return new VitalSignData(this.name, this.heartRate, this.oxygenVolumne, this.timestamp);
	}
	
	//Generating random data
	//<fileName> <#user> <#data per user> <interval (sec)>
	//starting date from now
	public static void main(String[] args) throws Exception{
		int dataTypeId = 1;
		PrintWriter writer = new PrintWriter(args[0]);
		int nUsers = new Integer(args[1]).intValue();
		int nDataPerUser = new Integer(args[2]).intValue();
		int secInterval = new Integer(args[3]).intValue();

		Date ts = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.parse("2011-01-01 00:00:00");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		System.out.println("Current time = " + ts);
		Calendar cal = Calendar.getInstance();
		cal.setTime(ts);
		Random random = new Random();
		String name;
		String time;
		for (int u = 0; u < nUsers; u++) {
			name = "user_"+u+"@gmail.com";
			for (int i = 0; i < nDataPerUser; i++) {
				cal.add(Calendar.SECOND, secInterval);
				time = df.format(cal.getTime());				
								
				Communicator comm = new Communicator(null, "localhost", 8080);
				comm.sendData(new VitalSignInfo(name, cal.getTime(),
						VitalSignData.HEART_RATE, random.nextInt(100)));
				return;
			}
		}
	}

	@Override
	public UploadID getUploadID() {
		return IDs.UploadID.Data;
	}
}
